<template>
  <j-modal
    :width="modalWidth"
    :visible="visible"
    :title="title"
    switchFullscreen
    @ok="handleSubmit"
    @cancel="close"
    style="top: 50px"
    cancelText="关闭"
  >
    <a-card :bordered="false">
      <a-row :gutter="8">
        <a-col :span="6">
          <div>
            <a-input-search v-model="searchValue" style="margin-bottom: 8px" placeholder="Search" />
            <a-tree :auto-expand-parent="true" :tree-data="gData" :replaceFields="replaceFields" @select="onSelect">
              <template #title="{ name }">
                <span v-if="name && name.indexOf(searchValue) > -1">
                  {{ name.substr(0, title.indexOf(searchValue)) }}
                  <span style="color: #f50">{{ searchValue }}</span>
                  {{ name.substr(title.indexOf(searchValue) + searchValue.length) }}
                </span>
                <span v-else>{{ name }}</span>
              </template>
            </a-tree>
          </div>
        </a-col>
        <a-col :span="18">
          <!-- 查询区域 -->
          <div class="table-page-search-wrapper">
            <a-form layout="inline" @keyup.enter.native="searchQuery">
              <a-row :gutter="24">
                <a-col :xl="6" :lg="7" :md="8" :sm="24">
                  <a-form-item label="名称">
                    <a-input placeholder="请输入物料名称" v-model="queryParam.materialName"></a-input>
                  </a-form-item>
                </a-col>
                <a-col :xl="6" :lg="7" :md="8" :sm="24">
                  <a-form-item label="物料编码">
                    <a-input placeholder="请输入物料编码" v-model="queryParam.materialCode"></a-input>
                  </a-form-item>
                </a-col>
                <a-col :xl="6" :lg="7" :md="8" :sm="24">
                  <a-form-item label="客户物料编码">
                    <a-input placeholder="请输入客户物料编码" v-model="queryParam.customerMaterialCode"></a-input>
                  </a-form-item>
                </a-col>
                <a-col :xl="6" :lg="7" :md="8" :sm="24">
                  <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                    <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                    <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                  </span>
                </a-col>
              </a-row>
            </a-form>
          </div>
          <!-- 查询区域-END -->
          <!-- table区域-begin -->
          <div>
            <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
              <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
              <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
              >项
              <a style="margin-left: 24px" @click="onClearSelected">清空</a>
            </div>

            <a-table
              ref="table"
              size="middle"
              :scroll="{ x: true }"
              bordered
              :rowKey="
                (record, index) => {
                  return record.id
                }
              "
              :columns="columns"
              :dataSource="dataSource"
              :pagination="ipagination"
              :loading="loading"
              :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: checkval }"
              class="j-table-force-nowrap1"
              @change="handleTableChange"
            />
          </div>
        </a-col>
      </a-row>
      <a-row :gutter="8">
        <a-col :span="24">
          <a-table
            ref="table1"
            size="middle"
            :scroll="{ x: true }"
            bordered
            :rowKey="
              (record, index) => {
                return record.id
              }
            "
            :columns="columns"
            :dataSource="selectionRows"
            :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: checkval }"
            class="j-table-force-nowrap1"
          />
        </a-col>
      </a-row>
    </a-card>
  </j-modal>
</template>
<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction } from '@api/manage'

export default {
  name: 'MaterialInventory',
  mixins: [JeecgListMixin, mixinDevice],
  components: {},
  props: {
    category: {
      type: Number,
      required: false,
      default: 0,
    },
    modalWidth: {
      type: Number,
      required: false,
      default: 1250,
    },
    checkval: {
      type: String,
      default: 'radio',
    },
    supplierId: {
      type: String,
      required: false,
    },
  },
  data() {
    return {
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
      },
      gData: [{ key: '1', title: '1', children: [{ key: '1-1', title: '1-1' }] }],
      replaceFields: {
        children: 'children',
        title: 'name',
        key: 'code',
      },
      searchValue: '',
      description: '物料基础信息管理页面',
      visible: false,
      title: '选择物料',
      disableMixinCreated: true,
      queryParam: { categoryCode: this.category },
      categoryCode: '',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: 'rowIndex',
          key: 'rowIndex',
          width: 40,
          align: 'center',
          fixed: 'left',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '物料编码',
          align: 'center',
          dataIndex: 'materialCode',
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName',
        },
        {
          title: '客户物料编码',
          align: 'center',
          dataIndex: 'customerMaterialCode',
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'specificationModel',
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText',
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'quantity',
        },
        {
          title: '辅单位',
          align: 'center',
          dataIndex: 'secondaryUnit_dictText',
        },
        {
          title: '辅数量',
          align: 'center',
          dataIndex: 'secondaryQuantity',
        },
        {
          title: '生产车间',
          align: 'center',
          dataIndex: 'productionShop_dictText',
        },
        {
          title: '生产日期',
          align: 'center',
          dataIndex: 'productionDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '保质期（天）',
          align: 'center',
          dataIndex: 'shelfLife',
        },
        {
          title: '限用日期',
          align: 'center',
          dataIndex: 'expirationDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '批次号',
          align: 'center',
          dataIndex: 'batchNumber',
        },
        {
          title: '物料批号',
          align: 'center',
          dataIndex: 'materialBatchNumber',
        },
        {
          title: '储位',
          align: 'center',
          dataIndex: 'storageLocation_dictText',
        },
        {
          title: '入库日期',
          align: 'center',
          dataIndex: 'entryDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '库龄（天）',
          align: 'center',
          dataIndex: 'inventoryAge',
        },
        {
          title: '质检状态',
          align: 'center',
          dataIndex: 'qualityInspectionStatus_dictText',
        },
        {
          title: '物料分类1',
          align: 'center',
          dataIndex: 'materialClassify_dictText',
        },
        {
          title: '物料分类2',
          align: 'center',
          dataIndex: 'brand_dictText',
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark',
          scopedSlots: { customRender: 'remark' },
        },
      ],
      url: {
        list: '/wms/inventory/materialInventoryPageList',
      },
    }
  },
  created() {
  },
  computed: {
  },
  watch: {
    category(newval, oldval) {
      this.loadGoodsCategory()
    },
  },
  mounted() {
    this.loadGoodsCategory()
  },
  methods: {
    loadGoodsCategory() {
      // 加载分类
      getAction('/goods/erpGoodsCategory/categorylist', { category: this.category }).then((res) => {
        if (res.success) {
          this.gData = res.result
        }
      })
    },
    onSelect(selectedKeys, e) {
      if(selectedKeys.length > 0){
        this.categoryCode = selectedKeys
      }
      this.queryParam = { categoryCode: selectedKeys }
      this.loadData()
    },
    handleSubmit() {
      this.getGoodsSelectRows()
      this.queryParam = {}
      this.onClearSelected()
      this.close()
    },
    add() {
      this.showModal()
    },
    showModal() {
      this.dataSource = []
      this.visible = true
    },
    getGoodsSelectRows() {
      let selectedRow = null
      let selectedRowKey=null
      if (this.checkval == 'radio') {
        selectedRow = this.selectionRows[0]
        selectedRowKey=this.selectedRowKey[0]
      } else {
        selectedRow = this.selectionRows
        selectedRowKey=this.selectedRowKeys
      }
      this.$emit('goodsOk', selectedRowKey)
      this.$emit('goodRowsOk',selectedRow)
    },
    initDictConfig() {},
    close() {
      this.queryParam = {}
      this.selectedRowKeys = []
      this.selectionRows = []
      this.visible = false
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() //查询条件
      if (this.categoryCode) {
        params.categoryNo = this.categoryCode
      }
      this.loading = true
      // 只查询合格的
      params.qualityInspectionStatus = 1
      getAction(this.url.list, params)
        .then((res) => {
          if (res.success) {
            this.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
